package com.example.leetcode;

import java.util.Arrays;

public class Demo0931 {
    public static void main(String[] args) {
        int [][] fun={{0,1,2},{5,3,4},{6,7,8}};
        int[] f=fun[0];
        System.out.println(f);
    }
    public int minFallingPathSum(int[][] matrix) {
        int len = matrix.length;
        int [][] fun=new int[len][len];
        fun[0]=matrix[0];
        for (int i = 1; i < len; i++) {
            for (int j = 0; j < len; j++) {
                if (j==0){
                    fun[i][j]=Math.min(matrix[i][j]+fun[i-1][j],matrix[i][j]+fun[i-1][j+1]);
                    continue;
                }
                if (j==len-1){
                    fun[i][j]=Math.min(matrix[i][j]+fun[i-1][j],matrix[i][j]+fun[i-1][j-1]);
                    continue;
                }
                fun[i][j]=Math.min(Math.min(matrix[i][j]+fun[i-1][j],matrix[i][j]+fun[i-1][j-1]),
                        matrix[i][j]+fun[i-1][j+1]);
            }
        }
        Arrays.sort(fun[len-1]);
        return fun[len-1][0];
    }
}
